package com.tfjybj.itoo.teach.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.teach.entity.ProfessionEntity;
import com.tfjybj.itoo.teach.model.ProfessionModel;
import com.tfjybj.itoo.teach.provider.service.ProfessionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * ProfessionController
 * profession表
 *
 * @author 田成荣
 * @version 2.0.0
 * @since 2.0.0 2018-11-08 19:34:10
 */
@Api(tags = {"profession表接口"})
@RequestMapping(value = "/profession")
@RestController
public class ProfessionController {

    @Resource
    private ProfessionService professionService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model ProfessionModel
     * @return 添加的结果（修改，加入判重）
     * @author 杨燕梅
     * @since 2.0.0 2018-12-20 14:41:24
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    @Transactional(rollbackFor = Exception.class)
    public ItooResult create(@RequestBody ProfessionModel model) {
		if (StringUtils.isEmpty(model.getName())){
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
		if (StringUtils.isEmpty(model.getCode())){
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        if (StringUtils.isEmpty(model.getNature())){
            return ItooResult.build(ItooResult.FAIL, "nature为空");
        }
        if (StringUtils.isEmpty(model.getSchoolLength())){
            return ItooResult.build(ItooResult.FAIL, "schoolLength为空");
        }
        if (StringUtils.isEmpty(model.getAcademyId())){
            return ItooResult.build(ItooResult.FAIL, "academyId为空");
        }
        //判断专业是否重复
        if (!CollectionUtils.isEmpty(professionService.queryByCodeAndNameExist(model.getCode(),model.getName()))){
            return ItooResult.build(ItooResult.FAIL, "专业已存在");
        }
        ProfessionEntity professionEntity = new ProfessionEntity();
        BeanUtils.copyProperties(model, professionEntity);
        professionService.save(professionEntity);
        professionService.saveToAuth(professionEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功          
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        professionService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        professionService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model ProfessionModel
     * @return 修改后的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id修改profession")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody ProfessionModel model) {
        if (StringUtils.isEmpty(model.getName())){
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())){
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        if (StringUtils.isEmpty(model.getNature())){
            return ItooResult.build(ItooResult.FAIL, "nature为空");
        }
        if (StringUtils.isEmpty(model.getSchoolLength())){
            return ItooResult.build(ItooResult.FAIL, "schoolLength为空");
        }
        if (StringUtils.isEmpty(model.getAcademyId())){
            return ItooResult.build(ItooResult.FAIL, "academyId为空");
        }
        ProfessionEntity professionEntity = new ProfessionEntity();
        BeanUtils.copyProperties(model, professionEntity);
        professionService.updateById(professionEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找Profession
     *
     * @param id 主键id
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        ProfessionEntity professionEntity = professionService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", professionEntity);
    }

    /**
     * 分页查询所有Profession
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "分页查询所有Profession")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize",value = "页数",required = true,example = "10")@PathVariable Integer pageSize) {
        PageInfo<ProfessionEntity> professions = professionService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", professions);
    }

    /**
     * 根据专业名称模糊查询profession
     *
     * @param name  专业名称
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的profession
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据专业名称模糊查询profession", notes = "分页根据专业名称模糊查询profession")
    @GetMapping(value = "queryProfessionByLikeName/{pageNo}/{pageSize}")
    public ItooResult queryProfessionByLikeName(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo,
                                                @ApiParam(name = "pageSize",value = "页数",required = true,example = "10")@PathVariable Integer pageSize,
                                                @RequestParam(required = false, defaultValue = "") String name) {
        PageInfo<ProfessionEntity> professionList = professionService.queryByLikeName(name, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", professionList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */
    /**
     * 根据教学部门id查询专业信息（带分页）
     * @param academyId 学院id
     * @param pageNo   页码
     * @param pageSize 页数
     * @return name  专业名称
     * @author 杨燕梅
     * @since 2.0.0 2018-11-15 15:43:42
     */
    @ApiOperation(value="根据教学部门id查询专业信息（带分页）",notes="分页根据学院id查询profession")
    @GetMapping(value = "queryProfessionByAcademyIdPageInfo/{pageNo}/{pageSize}/{academyId}")
    public ItooResult queryProfessionByAcademyIdPageInfo(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo,
                                                         @ApiParam(name = "pageSize",value = "页数",required = true,example = "10")@PathVariable Integer pageSize,
                                                         @ApiParam(name = "academyId",value = "教学部门id",required = true)@PathVariable String academyId){
        PageInfo<ProfessionModel> professionList = professionService.queryByAcademyIdPageInfo(academyId,pageNo,pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", professionList);
    }

    /**
     * 根据教学部门id查询专业信息
     * @param academyId 学院id
     * @return name  专业名称
     * @author 杨燕梅
     * @since 2.0.0 2018-11-18 09:11:31
     */
    @ApiOperation(value="根据教学部门id查询专业信息", notes = "根据教学部门id查询专业信息")
    @GetMapping(value = "queryProfessionByAcademyId/{academyId}")
    public ItooResult queryProfessionByAcademyId(@ApiParam(name = "academyId",value = "教学部门id",required = true)@PathVariable String academyId){
        List<ProfessionModel> professionList = professionService.queryByAcademyId(academyId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", professionList);
    }

    /**
     * 根据专业名称模糊查询professions
     * @param name  专业名称
     * @return 查询的profession
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据专业名称查询profession", notes = "分页根据专业名称查询profession")
    @GetMapping(value = "queryProfessionsByName")
    public ItooResult queryProfessionsByName(@RequestParam(required = false, defaultValue = "") String name) {
        List<ProfessionModel> professionList = professionService.queryProfessionsByName(name);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", professionList);
    }

    /**
     * 查询所有专业层次
     *
     * @return nature  专业层次
     * @author 杨燕梅
     * @since 2.0.0 2018-11-28 14:59:20
     */
    @ApiOperation(value = "查询所有专业层次", notes = "查询所有专业层次")
    @GetMapping(value = {"queryAllNature"})
    public ItooResult queryAllNature() {
        List<ProfessionModel> natureList = professionService.queryAllNature();
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", natureList);
    }

    /**
     * 查询所有学制
     *
     * @return schoolLength  学制
     * @author 杨燕梅
     * @since 2.0.0 2018-11-28 14:59:40
     */
    @ApiOperation(value = "查询所有学制", notes = "查询所有学制")
    @GetMapping(value = {"queryAllSchoolLength"})
    public ItooResult queryAllSchoolLength() {
        List<ProfessionModel> schoolLengthList = professionService.queryAllSchoolLength();
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", schoolLengthList);
    }

    /**
     * 根据专业名称模糊查询某一学院下属专业
     *
     * @param searchContent  专业名称
     * @param pageNo   页码
     * @param pageSize 页数
     * @param academyId 学院id
     * @return 模糊查询的某一学院下属专业
     * @author 杨燕梅
     * @since 2.0.0 2018-11-28 20:21:04
     */
    @ApiOperation(value = "根据专业名称模糊查询某一学院下属专业", notes = "根据专业名称模糊查询某一学院下属专业")
    @GetMapping(value = "queryProfessionByLikeNameAndAcademyId/{academyId}/{pageNo}/{pageSize}")
    public ItooResult queryProfessionByLikeNameAndAcademyId(@ApiParam(name = "academyId",value = "学院id",required = true)@PathVariable String academyId,
                                                            @ApiParam(name = "pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo,
                                                            @ApiParam(name = "pageSize",value = "页数",required = true,example = "10")@PathVariable Integer pageSize,
                                                            @RequestParam(required = false, defaultValue = "") String searchContent)
    {
        PageInfo<ProfessionModel> professionList = professionService.queryByLikeNameAndAcademyId(academyId, pageNo, pageSize,searchContent);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", professionList);
    }

    /**
     * 根据专业ID查询专业详情
     *
     * @param idList 专业ID
     * @return 专业实体集合
     * @author 杨燕梅
     * @since 2.0.0 2018-12-12 20:04:33
     */
    @ApiOperation(value = "根据专业ID查询专业详情")
    @GetMapping(value = "/queryProfessionById/{idList}")
    public ItooResult queryProfessionById(@ApiParam(name = "idList", value = "专业ID", required = true) @PathVariable List<String> idList) {
        List<ProfessionModel> professionModelList =professionService.queryProfessionById(idList);
        return ItooResult.build(ItooResult.SUCCESS, "查询专业成功!", professionModelList);
    }

    /**
     * 根据ID导出专业信息
     *
     * @param response 响应
     * @param idList   专业ID
     * @return 导出结果信息
     * @author 杨燕梅
     * @since 2.0.0 2018-12-12 20:04:39
     */
    @ApiOperation(value = "根据ID导出专业信息", notes = "根据ID导出专业信息")
    @GetMapping(value = {"/exportAll"})
    public ItooResult exportAll(HttpServletResponse response, @RequestParam(required = false, defaultValue = "") List<String> idList) {
        return professionService.exportByProfessionId(response, idList);
    }

    /**
     * 根据条件导出专业
     *
     * @param response     响应
     * @param searchContent    专业名称/专业简称
     * @return 导出结果信息
     * @author 杨燕梅
     * @since 2.0.0 2018-12-23 16:49:39
     */
    @ApiOperation(value = "根据条件导出专业", notes = "根据条件导出专业")
    @GetMapping(value = {"/exportByStrLike/{academyId}"})
    public ItooResult exportByStrLike(HttpServletResponse response, @ApiParam(name = "academyId", value = "学院Id", required = true) @PathVariable String academyId, @RequestParam(required = false, defaultValue = "") String searchContent) {
        return professionService.exportByStrLike(response,academyId,searchContent);
    }

    /**
     * 下载专业模板，批量导入使用
     *
     * @param response 响应
     * @return 下载模板是否成功
     * @author 杨燕梅
     * @since 2.0.0 2018-11-30 10:24:09
     */
    @ApiOperation(value = "下载专业模板，批量导入使用", notes = "下载专业模板，批量导入使用")
    @GetMapping(value = {"/downLoadProfessionTemplate"})
    public ItooResult downLoadProfessionTemplate(HttpServletResponse response) {
        if (professionService.downLoadTemplate(response)) {
            return ItooResult.build(ItooResult.SUCCESS, "下载专业模板成功");
        } else {
            return ItooResult.build(ItooResult.FAIL, "下载专业模板失败");
        }
    }

    /**
     * 批量导入专业
     *
     * @param multipartFile 批量导入的文件
     * @param request       请求
     * @param response      响应
     * @return ItooResult 批量导入的结果
     * @author 杨燕梅
     * @since 2.0.0 2018-11-30 11:17:29
     */
    @ApiOperation(value = "批量导入专业", notes = "0001:部分成功；0000:成功；1111:失败")
    @PostMapping(value = {"/importProfessionInfoByTemplate"})
    public ItooResult importProfessionInfoByTemplate(@RequestParam(value = "file") MultipartFile multipartFile,
                                                     HttpServletRequest request, HttpServletResponse response) {
        return professionService.importTemplate(multipartFile, request, response);
    }

    /**
     * 根据errorListId导出未成功导入的专业信息
     *
     * @param response    响应
     * @param errorListId 为成功导入的分组id
     * @return 导出结果
     * @author 杨燕梅
     * @since 1.0.0 2018-12-2 10:23:20
     */
    @ApiOperation(value = "根据errorListId导出未成功导入的专业信息", notes = "根据errorListId导出未成功导入的专业信息")
    @GetMapping(value = {"/exportErrorProfessionById/{errorListId}"})
    public ItooResult exportErrorProfessionById(HttpServletResponse response,
                                                @ApiParam(name = "errorListId", value = "errorListId", required = true) @PathVariable String errorListId) {
        if (professionService.exportErrorList(errorListId, response)) {
            return ItooResult.build(ItooResult.SUCCESS, "导出成功");
        } else {
            return ItooResult.build(ItooResult.FAIL, "导出失败");
        }
    }

    /**
     * 根据专业性质（专业层次）汉字（本科，专科，专升本）查询相应代码
     * @param name  专业性质（专业层次）汉字
     * @return 专业层次的相应代码
     * @author 杨燕梅
     * @since 2.0.0 2018-12-2 20:28:43
     */
    @ApiOperation(value = "根据专业性质（专业层次）汉字（本科，专科，专升本）查询相应代码", notes = "根据专业性质（专业层次）汉字（本科，专科，专升本）查询相应代码")
    @GetMapping(value = {"queryNatureIdByName"})
    public ItooResult queryNatureIdByName(@PathVariable String name) {
        String professionModel = professionService.queryNatureIdByName(name);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", professionModel);
    }

    /**
     * 根据专业代码和专业名称查询专业是否已存在
     *
     * @param code   专业代码
     * @param name   专业名称
     * @return 专业信息
     * @author 杨燕梅
     * @since 2.0.0 2018-12-20 09:49:06
     */
    @ApiOperation(value = "根据专业代码和专业名称查询专业是否已存在", notes = "根据专业代码和专业名称查询专业是否已存在")
    @GetMapping(value = {"/queryByCodeAndNameExist/{code}/{name}"})
    public ItooResult queryByCodeAndNameExist(@ApiParam(name = "code", value = "专业代码", required = true) @PathVariable String code,
                                              @ApiParam(name = "name", value = "专业名称", required = true) @PathVariable String name) {
        List<ProfessionEntity> professionList = professionService.queryByCodeAndNameExist(code,name);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", professionList);
    }

}
